/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.PhieuMuaHangDTO;

/**
 *
 * @author tuan
 */
public class PhieuMuaHangDAO {
    private static final String MaPhieuMH = "MaPMH000";
    private static final int lengthMa = 8;
    public PhieuMuaHangDAO() {
    }
    
    public String getNewMaPhieuMuaHang() throws SQLException {
        String MaPMH = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllPhieuMuaHangFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAPHIEUMH").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaPMH = MaPMH.concat(MaPhieuMH + maxMa);
        return MaPMH;
    }
    
    public ArrayList<PhieuMuaHangDTO> SearchPhieuMuaHangByRule(PhieuMuaHangDTO phieuMH) throws SQLException {
        ArrayList<PhieuMuaHangDTO> listPhieuMH = new ArrayList<>();
        Object[] objs = { phieuMH.getMaPhieuMH(), phieuMH.getMaKhachHang(), phieuMH.getNgayMua(), 
                    phieuMH.getTongTien()};
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetPhieuMuaHangFromDatabase(?, ?, ?, ?)", objs);
        PhieuMuaHangDTO phieumuahang;
        while (result.next()) {
            phieumuahang = new PhieuMuaHangDTO(result.getString("MAPHIEUMH"), result.getString("MAKHACHANG"), 
                    result.getDate("NGAYMUA"), result.getLong("TONGTIEN"));
            listPhieuMH.add(phieumuahang);
        }
        return listPhieuMH;
    }
    
    public ArrayList<PhieuMuaHangDTO> GetPhieuNhapHangByMaPhieuNH(String MaPhieuMH) throws SQLException {
        ArrayList<PhieuMuaHangDTO> listPhieuMH = new ArrayList<>();
        Object[] objs = { MaPhieuMH };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetPhieuMuaHangByMaPhieuMHDatabase( ? )", objs);
        PhieuMuaHangDTO phieumuahang;
        while (result.next()) {
            phieumuahang = new PhieuMuaHangDTO(result.getString("MAPHIEUMH"), result.getString("MAKHACHHANG"),  result.getDate("NGAYMUA"),
                        result.getLong("TONGTIEN"));
            listPhieuMH.add(phieumuahang);
        }
        return listPhieuMH;
    }
        
    public int UpdatePhieuMuaHang(PhieuMuaHangDTO phieuMH) {
        Object[] objs = { phieuMH.getMaPhieuMH(), phieuMH.getMaKhachHang(), phieuMH.getNgayMua(), 
                    phieuMH.getTongTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdatePhieuMuaHangToDatabase(? ,?, ?, ?)", objs);
    }

    public int DeletePhieuMuaHang(PhieuMuaHangDTO phieuMH) {
        Object[] objs = { phieuMH.getMaPhieuMH()};
        return SQLServerConnect.getInstance().updateDataToDatabase("DeletePhieuMuaHangToDatabase(? )", objs);
    }
    
    public int InsertPhieuMuaHang(PhieuMuaHangDTO phieuMH) {
        Object[] objs = { phieuMH.getMaPhieuMH(), phieuMH.getMaKhachHang(), phieuMH.getNgayMua(), 
                    phieuMH.getTongTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertPhieuMuaHangToDatabase(? ,?, ?, ?)", objs);
    }
}
